var ranks = new Array();
ranks["Pawn"]="&#9823;";
ranks["Rook"]="&#9820;";
ranks["Knight"]="&#9822;";
ranks["Bishop"]="&#9821;";
ranks["Queen"]="&#9819;";
ranks["King"]="&#9818;";
var _24H = 24*60*60;
var depth;


$(document).ready(function() {
	$('table#board td:not(.side)').click(function() {
		selectChessPiece(this);
	});
	var gameId = retrieveCookie("ROGVchess");
	if (gameId != null) {
		gameInfo = new Array();
		gameInfo["id"] = gameId;
		gameInfo["lastMoveTimestamp"] = 0;
		pingRequest();
	}else {
		newGameRequest();
	}
});
function waitForComputer() {
	setTimeout('pingRequest()',1000);
}
function pingRequest() {
	$.ajax({
		url: './manager',
		type: 'POST',
		data: {action: "ping", gameId: gameInfo["id"] , lastMoveTimestamp : gameInfo["lastMoveTimestamp"]},
		dataType: "json",
		success: function(result) {
			if (result != null) {
				init(result);
			}else {
				console.debug("nothing has changed");
				waitForComputer();
			}
		},
		error: function(data, statusCode) {
			if (statusCode == 600 || statusCode== 601) {
				logInfo("No game found - Start a new game");
				return;
			}
			alert("ERROR: "+data + "" + statusCode);
		}
	});
}
function newGameRequest() {
	depth = document.selection.AI_level.value;
	$.ajax({
		url: './manager',
		type: 'POST',
		data: {action: "newGame", depth: depth},
		dataType: "json",
		success: function(result) {
			init(result);
		},
		error: function(data, statusCode) {
			if (statusCode == 600 || statusCode== 601) {
				logInfo("No game found - Start a new game");
				return;
			}
			alert("ERROR: "+data + "" + statusCode);
		}
	});
}

function moveRequest(move) {
	$.ajax({
		url: './manager',
		type: 'POST',
		data: {action: "move", gameId: gameInfo["id"], move : move},
		dataType: "json",
		success: function(result) {
			init(result);
			if (result["additionalInfo"] != "GameOver") {
				waitForComputer();
			}else {
				logInfo('Game over - ' + gameInfo["status"]);
			}
		},
		error: function(data, statusCode) {
			if (statusCode == 600 || statusCode== 601) {
				logInfo("No game found - Start a new game");
				return;
			}
			alert("ERROR: "+data + "" + statusCode);
		}
	});
}
function logInfo(info) {
	$('input#logInfo').val(info);	
}
function init(serverResponse) {
	gameInfo=serverResponse["gameInfo"];
	setCookie("ROGVchess", gameInfo["id"],_24H);
	buildTable();
	setWaitingFor(gameInfo["waitingFor"]);
	console.debug(gameInfo["gameStatus"]);
}

function buildTable() {
	var pieces = gameInfo['board'];
	var tableRows = $('table#board tr');
	for (var i = 0; i < 8; i++) {
		var tableRow = $(tableRows[i]);
		var rowNum = 8 - i;
		for (var j=0;j<8;j++) {
			var tableCell = $(tableRow.find('td')[j]);
			tableCell.removeClass("white");
			tableCell.removeClass("black");
			var piece = pieces[rowNum - 1][j];
			
			if (piece == null) {
				tableCell.html("");
				continue;
			}
			var tmp = piece.split('_');
			
			tableCell.html(ranks[tmp[1]]);
			if ("white" == tmp[0]) {
				tableCell.addClass("white");
			}else {
				tableCell.addClass("black");
			}
		}
	}
}